package com.ruoyi.lead.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.lead.domain.TbLead;
import com.ruoyi.lead.service.ITbLeadService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 线索Controller
 * 
 * @author ruoyi
 * @date 2025-10-19
 */
@RestController
@RequestMapping("/lead/lead")
public class TbLeadController extends BaseController
{
    @Autowired
    private ITbLeadService tbLeadService;

    /**
     * 查询线索列表
     */
    @PreAuthorize("@ss.hasPermi('lead:lead:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询线索列表")
    public TableDataInfo list(TbLead tbLead)
    {
        startPage();
        List<TbLead> list = tbLeadService.selectTbLeadList(tbLead);
        return getDataTable(list);
    }

    /**
     * 导出线索列表
     */
    @PreAuthorize("@ss.hasPermi('lead:lead:export')")
    @Log(title = "线索", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出线索列表")
    public void export(HttpServletResponse response, TbLead tbLead)
    {
        List<TbLead> list = tbLeadService.selectTbLeadList(tbLead);
        ExcelUtil<TbLead> util = new ExcelUtil<TbLead>(TbLead.class);
        util.exportExcel(response, list, "线索数据");
    }

    /**
     * 获取线索详细信息
     */
    @PreAuthorize("@ss.hasPermi('lead:lead:query')")
    @GetMapping(value = "/{leadId}")
    @ApiOperation(value = "获取线索详细信息")
    public AjaxResult getInfo(@PathVariable("leadId") Long leadId)
    {
        return success(tbLeadService.selectTbLeadByLeadId(leadId));
    }

    /**
     * 新增线索
     */
    @PreAuthorize("@ss.hasPermi('lead:lead:add')")
    @Log(title = "线索", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增线索")
    public AjaxResult add(@RequestBody TbLead tbLead)
    {
        return toAjax(tbLeadService.insertTbLead(tbLead));
    }

    /**
     * 修改线索
     */
    @PreAuthorize("@ss.hasPermi('lead:lead:edit')")
    @Log(title = "线索", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改线索")
    public AjaxResult edit(@RequestBody TbLead tbLead)
    {
        return toAjax(tbLeadService.updateTbLead(tbLead));
    }

    /**
     * 删除线索
     */
    @PreAuthorize("@ss.hasPermi('lead:lead:remove')")
    @Log(title = "线索", businessType = BusinessType.DELETE)
	@DeleteMapping("/{leadIds}")
    @ApiOperation(value = "删除线索")
    public AjaxResult remove(@PathVariable Long[] leadIds)
    {
        return toAjax(tbLeadService.deleteTbLeadByLeadIds(leadIds));
    }

    /**
     * 导入
     * @param file
     * @return
     */
    @PostMapping("leadImport")
    @ApiOperation(value = "导入")
    public AjaxResult leadImport(MultipartFile file){
        tbLeadService.leadImport(file);
        return AjaxResult.success();
    }

    /**
     * 分配
     * @param params
     * @return
     */
    @PostMapping("/allocate")
    @ApiOperation(value = "分配")
    public AjaxResult allocateLead(@RequestBody Map<String, Object> params) {
        List<Long> leadIds = (List<Long>) params.get("leadIds");
        boolean result = tbLeadService.allocateLead(leadIds);
        return result ? AjaxResult.success() : AjaxResult.error("分配失败");
    }
}
